package com.hdax.controller;

import com.hdax.entity.Contractfile;
import com.hdax.service.ContractfileService;
import com.hdax.utils.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同文件控制器
 *
 * @author Aizen
 * @date 2022/05/21
 */
@Controller
@RequestMapping("/contract")
public class ContractFileController {

    /**
     * 合同文件服务
     */
    @Autowired
    private ContractfileService contractfileService;

    /**
     * 查询所有合同文件
     *
     * @return
     */
    @GetMapping("/list")
    public String contracts() {
        return "archives/ContractProfiles";
    }

    /**
     * 根据名称模糊查询
     * @param contractname
     * @return
     */
    @PostMapping(path = "/conList")
    @ResponseBody
    public Map<String,Object> contracts(@RequestParam("page")int page, @RequestParam("limit")int limit, @Param("contractname")String contractname){
        List<Contractfile> contractFileList = contractfileService.findByName(page-1,limit,contractname);
        Map<String,Object> map = new HashMap<String,Object>();
        //集合
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", contractfileService.countByName(contractname));
        map.put("data", contractFileList);
        return map;
    }


    //---------------------------------------------------


    /**
     * 添加合同
     *
     * @return {@link String}
     */
    @GetMapping(path = "/addContract")
    public String addContract(){
        return "archives/addContract";
    }

    /**
     * 提交添加合同
     *
     * @param contractfile contractfile
     * @return {@link ResultUtil}
     */
    @ResponseBody
    @PostMapping(path = "/addContract")
    public ResultUtil addContract(Contractfile contractfile){
        return contractfileService.addContract(contractfile);
    }

    //-----------------------------------------------------------------显示合同签订列表

    /**
     * 显示合同签订列表
     *
     * @return {@link String}
     */
    @GetMapping("/showContract")
    public String showContract(){
        return "archives/showContract";
    }

    //-------------------

    /**
     * 根据ID查看合同详情(跳转页面）
     *
     * @return {@link ModelAndView}
     */
    @GetMapping("/showConAndEmployee")
    public String showConById(@RequestParam("id")int id, Map<Object,Object> map){
        Contractfile contractfile = contractfileService.findById(id);
        map.put("id",id);
        map.put("contractName",contractfile.getContractName());
        map.put("text",contractfile.getText());
        return "archives/showContract";
    }

    @ResponseBody
    @PostMapping("/showConAndEmployee/{id}")
    public ResultUtil showConById(@PathVariable("id")int id){
        return contractfileService.findConAndEmployee(id);
    }






    //-------------------------------------------------------------------------删除合同

    /**
     * 删除
     *
     * @param conId 合同id
     * @return {@link ResultUtil}
     */
    @ResponseBody
    @PostMapping("/delete")
    public ResultUtil delete(Integer conId){
        System.out.println(conId);
        return contractfileService.deleteCon(conId);
    }


    /**
     * 导出合同
     */
    @GetMapping("/export")
    @ResponseBody
    public Boolean daoChu(Integer conId){
        return contractfileService.export(conId);
    }









}
